Remove dependency on async library#555
Conversation
44342fe to
2c540dd
Compare
| var ACL = require('../acl-checker') | ||
| var $rdf = require('rdflib') | ||
| var url = require('url') | ||
| var async = require('async') |
| readFile(uri, host, ldp, baseUri).then(body => { | ||
| const graph = $rdf.graph() | ||
| $rdf.parse(body, graph, uri, 'text/turtle') | ||
| return graph |
There was a problem hiding this comment.
Mostly unrelated to this PR, but these 3 lines come up again and again when working with rdflib.js. I'd love to have an alternative API which takes a string and returns a graph.
| async.each(matches, function (match, done) { | ||
| Promise.all(matches.map(match => new Promise((resolve, reject) => { | ||
| var baseUri = utils.filenameToBaseUri(match, reqOrigin, root) | ||
| fs.readFile(match, {encoding: 'utf8'}, function (err, fileData) { |
There was a problem hiding this comment.
Again probably not in scope for the PR, but I'm confused as to why we need a readFile declared here vs fs.readFile.
There was a problem hiding this comment.
Yeah, unsure. I just ported it.
| var stringToStream = require('./utils').stringToStream | ||
| var serialize = require('./utils').serialize | ||
| var extend = require('extend') | ||
| var doWhilst = require('async').doWhilst |
| // add container stats | ||
| new Promise((resolve, reject) => | ||
| ldpContainer.addContainerStats(ldp, reqUri, filename, resourceGraph, | ||
| err => err ? reject(err) : resolve()) |
There was a problem hiding this comment.
Just a nitpick comment (don't need to address) but the indentation here through me off as to which expression the anonymous function was being passed off to.
There was a problem hiding this comment.
I can address this, as there are some other points as well. What indentation would you use here?
There was a problem hiding this comment.
It's definitely a matter of taste but this is how I generally like things: when breaking function arguments up into several lines, I like to keep the closing paren ) on its own line, indented to match the start of the line of the function call.
For example:
ldpContainer.addContainerStats(
ldp, reqUri, filename, resourceGraph,
err => err ? reject(err) : resolve()
)I also see this a lot when using callback functions - breaking the line after the arguments to the callback:
ldpContainer.addContainerStats(ldp, reqUri, filename, resourceGraph, err =>
err ? reject(err) : resolve()
)There was a problem hiding this comment.
We should really consider using a linter that goes beyond standard. There are still lots of variations in the code.
This particular instance can perhaps be solved with promisify (see below).
There was a problem hiding this comment.
Yeah, fair point. A code formatter would be nice. I thought I saw you discussing prettier at some point in gitter. I'd be in favor of adding that, for no other reason than not having to think about style ever again :)
There was a problem hiding this comment.
Won't have been me 😄 I'm in favor of a strict .eslintrc (and really dislike standard 😉).
There was a problem hiding this comment.
I'm leaving this as-is, since other parts in the same file follow the same indentation.
There was a problem hiding this comment.
That's fine. Consistency is more important :)
| // read directory | ||
| .then(() => new Promise((resolve, reject) => | ||
| ldpContainer.readdir(filename, | ||
| (err, files) => err ? reject(err) : resolve(files)) |
There was a problem hiding this comment.
It seems like a node convention for pre-Promise API async functions to take callbacks which themselves take a possible error as the first argument and a successful value as the next argument. We could write a higher-order function to make these kinds of functions return promises:
const promisify = (fn) =>
(...args) =>
new Promise((resolve, reject) =>
fn(...args, (err, success) =>
err
? reject(err)
: resolve(success)
)
)There was a problem hiding this comment.
We could do that—and there are a couple of libraries that expose such a function (starting with Node 8 even in core). I've used that in other projects.
I was hesitant to do this here and in other places of the Solid code, because callbacks are sometimes used inconsistently (they don't always have an error argument). So I could start using it here, but then it would perhaps be good to keep an eye on it in other places of the code as well. And eventually, we should migrate to an await/async style, I suppose.
So, what do you think, should we start the promisify practice here?
Perhaps we should consider switching to async/await (with build step) for node-solid-server v5? Or already start it here?
There was a problem hiding this comment.
Good points!
I was hesitant to do this here and in other places of the Solid code, because callbacks are sometimes used inconsistently (they don't always have an error argument).
Oof yeah that complicates things!
And eventually, we should migrate to an await/async style, I suppose.
Sure thing. Though async/await is syntactic sugar on top of Promises, so I think that "promisifying" our interfaces goes hand-in-hand with async/await-ifying them :)
I like your point that this change is more of an interface redesign than simply "promisifying" the existing APIs. How about we simply leave things as they are here, and I'll create a ticket for making the async APIs more consistent.
There was a problem hiding this comment.
👍 I'll make the other changes and merge.
| nock('https://example.org').get('/404').reply(404) | ||
| nock('https://example.org').get('/401').reply(401) | ||
| nock('https://example.org').get('/500').reply(500) | ||
| nock('https://example.org').get('/200').reply(200) |
There was a problem hiding this comment.
You can replace these four lines with:
nock('https://example.org')
.get('/404').reply(404)
.get('/401').reply(401)
.get('/500').reply(500)
.get('/200').reply(200) There was a problem hiding this comment.
Thanks, will do.
|
Just a few suggestions, but no blockers from me. 👍, nice work! |
2c540dd to
d54f958
Compare
No description provided.